//
//  CustomNavBar.m
//  SlimmingWar
//
//  Created by kai on 12-3-17.
//  Copyright (c) 2012年 110. All rights reserved.
//

/********************************
自定义NavigationBar
*********************************/


#import "CustomNavBar.h"

@implementation CustomNavBar
//@synthesize navigationController,navigationBarBackgroundImage;
@synthesize allExLab,lvLab,goldLab,database,selectGoldImg;
- (UIImageView *) selectImg
{
    selectGoldImg = [[UIImageView alloc] init];
    
    NSString *gold;
//    int gold;
    NSString *dbName = @"/UserInfo.db";
    NSString *sourcePath = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], dbName];
    database = [FMDatabase databaseWithPath:sourcePath];
    if( ![database open] ){
        NSLog(@"could not open database");
        [database release];
    }
    else{
        NSLog(@"open database ok");
    }
    NSString *query = @"select * from UserInfoEx";
    FMResultSet *rs = [database executeQuery:query];
    if (!rs)
    {
        NSLog(@"ERROR:LINE:%d Func:%s", __LINE__, __FUNCTION__);
    }    
    while ([rs next]) 
    {
        gold = [rs stringForColumn:@"userGold"];
    }
    
    switch ([gold length]) {
        case 1:
            selectGoldImg.frame = CGRectMake(44, 8, 18, 15);
            selectGoldImg.image = [UIImage imageNamed:@"nav_Gold1.png"];
            break;
        case 2:
            selectGoldImg.frame = CGRectMake(44, 8, 22, 15);
            selectGoldImg.image = [UIImage imageNamed:@"nav_Gold2.png"];
        case 3:
            selectGoldImg.frame = CGRectMake(44, 8, 27, 15);
            selectGoldImg.image = [UIImage imageNamed:@"nav_Gold3.png"];
        case 4:
            selectGoldImg.frame = CGRectMake(44, 8, 32, 15);
            selectGoldImg.image = [UIImage imageNamed:@"nav_Godl4.png"];
        case 5:
            selectGoldImg.frame = CGRectMake(44, 8, 37, 15);
            selectGoldImg.image = [UIImage imageNamed:@"nav_Gold5.png"];
        case 6:
            selectGoldImg.frame = CGRectMake(44, 8, 42, 15);
            selectGoldImg.image = [UIImage imageNamed:@"nav_Gold6.png"];
        default:
            break;
        
    }
    
    [rs close];
    [database close];
    return selectGoldImg;
}

- (void) awakeFromNib
{
//    self.frame = CGRectMake(0, 0, 320, 30);
    selectGoldImg = [self selectImg];
    CGRect frame = self.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    frame.size = CGSizeMake(320, 30);
    //打开数据库
    NSString *dbName = @"/UserInfo.db";
    NSString *sourcePath = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], dbName];
    database = [FMDatabase databaseWithPath:sourcePath];
    if( ![database open] ){
        NSLog(@"could not open database");
        [database release];
    }
    else{
        NSLog(@"open database ok");
    }
    

    UIImageView *nav_bar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 31)];    
    nav_bar.image = [UIImage imageNamed:@"nav_bar.png"];
    [self addSubview:nav_bar];
    
    //定制金币图标
    UIImageView *gold = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 20, 20)];
    gold.image = [UIImage imageNamed:@"gold.png"];
    [self addSubview:gold];
    [gold release];
    
    //定制金币数的背景
    UIImageView *nav_gold = selectGoldImg;
    [self addSubview:nav_gold];
    [nav_gold release];

    //定制等级标签
    UILabel *lv = [[UILabel alloc] initWithFrame:CGRectMake(100, 8, 30, 15)];
    lv.backgroundColor = [UIColor clearColor];
    lv.text = @"等级";
    lv.font = [UIFont systemFontOfSize:12];
    lv.textColor = [UIColor orangeColor];
    [self addSubview:lv];
    [lv release];
            
    //定制等级标签数字背景
    UIImageView *nav_Level = [[UIImageView alloc] initWithFrame:CGRectMake(130, 9, 18, 13)];
    nav_Level.image = [UIImage imageNamed:@"nav_Level.png"];
    [self addSubview:nav_Level];
    [nav_Level release];
        
    //定制经验标签
    UILabel *ex = [[UILabel alloc] initWithFrame:CGRectMake(167, 8, 30, 15)];
    ex.backgroundColor = [UIColor clearColor];
    ex.text = @"经验";
    ex.font = [UIFont systemFontOfSize:12];
    ex.textColor = [UIColor orangeColor];
    [self addSubview:ex];
    [ex release];
    
    //金钱数标签
    goldLab = [[UILabel alloc] initWithFrame:CGRectMake(50, 8, 50, 15)];
    goldLab.backgroundColor = [UIColor clearColor];
    
    //从数据库中读取数据
    NSString *query = @"select * from UserInfoEx";
    FMResultSet *rs = [database executeQuery:query];
    if (!rs)
    {
        NSLog(@"ERROR:LINE:%d Func:%s", __LINE__, __FUNCTION__);
        return;
    }    
    while ([rs next]) 
    {
        goldLab.text = [rs stringForColumn:@"userGold"];
    }
    
    goldLab.textColor = [UIColor whiteColor];
    goldLab.font = [UIFont systemFontOfSize:13];
    [self addSubview:goldLab];
        
    //等级数标签
    lvLab = [[UILabel alloc] initWithFrame:CGRectMake(135, 9, 50, 13)];
    lvLab.backgroundColor = [UIColor clearColor];
    
    //从数据库中读取数据
    query = @"select * from UserInfoEx";
    rs = [database executeQuery:query];
    if (!rs)
    {
        NSLog(@"ERROR:LINE:%d Func:%s", __LINE__, __FUNCTION__);
        return;
    }    
    while ([rs next]) 
    {
        lvLab.text = [rs stringForColumn:@"userLevel"];
    }
    
    lvLab.textColor = [UIColor whiteColor];
    lvLab.font = [UIFont systemFontOfSize:13];
    [self addSubview:lvLab];
    
    //当前经验标签
    allExLab = [[UILabel alloc] initWithFrame:CGRectMake(200, 9, 100, 13)];
    allExLab.backgroundColor = [UIColor clearColor];
    
    //从数据库中读取数据
    query = @"select * from UserInfoEx";
    rs = [database executeQuery:query];
    if (!rs)
    {
        NSLog(@"ERROR:LINE:%d Func:%s", __LINE__, __FUNCTION__);
        return;
    }    
    while ([rs next]) 
    {
        NSString *allEx = [rs stringForColumn:@"userExpAll"];
        NSLog(@"%@",[rs stringForColumn:@"userExpAll"]);
        NSLog(@"%@",[[[rs stringForColumn:@"userExp"] stringByAppendingString:@"/"] stringByAppendingString:allEx]);
        allExLab.text = [[[rs stringForColumn:@"userExp"] stringByAppendingString:@"/"] stringByAppendingString:allEx];
    }
    
    allExLab.textColor = [UIColor orangeColor];
    allExLab.font = [UIFont systemFontOfSize:13];
    [self addSubview:allExLab];
    
    [rs close];
    [database close];
    
    
}



//- (void)drawRect:(CGRect)rect
//{
//    if (navigationBarBackgroundImage)
//        [navigationBarBackgroundImage.image drawInRect:rect];
//    else
//        [super drawRect:rect];
//}
//
//// Save the background image and call setNeedsDisplay to force a redraw
//-(void) setBackgroundWith:(UIImage*)backgroundImage
//{
//    self.navigationBarBackgroundImage = [[[UIImageView alloc] initWithFrame:self.frame] autorelease];
//    navigationBarBackgroundImage.image = backgroundImage;
//    [self setNeedsDisplay];
//}
//
//// clear the background image and call setNeedsDisplay to force a redraw
//-(void) clearBackground
//{
//    self.navigationBarBackgroundImage = nil;
//    [self setNeedsDisplay];
//}
//
//// With a custom back button, we have to provide the action. We simply pop the view controller
//- (IBAction)back:(id)sender
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
//
//// Given the prpoer images and cap width, create a variable width back button
//-(UIButton*) backButtonWith:(UIImage*)backButtonImage highlight:(UIImage*)backButtonHighlightImage leftCapWidth:(CGFloat)capWidth
//{
//    // store the cap width for use later when we set the text
//    backButtonCapWidth = capWidth;
//    
//    // Create stretchable images for the normal and highlighted states
//    UIImage* buttonImage = [backButtonImage stretchableImageWithLeftCapWidth:backButtonCapWidth topCapHeight:0.0];
//    UIImage* buttonHighlightImage = [backButtonHighlightImage stretchableImageWithLeftCapWidth:backButtonCapWidth topCapHeight:0.0];
//    
//    // Create a custom button
//    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    // Set the title to use the same font and shadow as the standard back button
//    button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
//    button.titleLabel.textColor = [UIColor whiteColor];
//    button.titleLabel.shadowOffset = CGSizeMake(0,-1);
//    button.titleLabel.shadowColor = [UIColor darkGrayColor];
//    
//    // Set the break mode to truncate at the end like the standard back button
//    button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
//    
//    // Inset the title on the left and right
//    button.titleEdgeInsets = UIEdgeInsetsMake(0, 6.0, 0, 3.0);
//    
//    // Make the button as high as the passed in image
//    button.frame = CGRectMake(0, 0, 0, buttonImage.size.height);
//    
//    // Just like the standard back button, use the title of the previous item as the default back text
//    [self setText:self.topItem.title onBackButton:button];
//    
//    // Set the stretchable images as the background for the button
//    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
//    [button setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
//    [button setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];
//    
//    // Add an action for going back
//    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
//    
//    return button;
//}
//
//// Set the text on the custom back button
//-(void) setText:(NSString*)text onBackButton:(UIButton*)backButton
//{
//    // Measure the width of the text
//    CGSize textSize = [text sizeWithFont:backButton.titleLabel.font];
//    // Change the button's frame. The width is either the width of the new text or the max width
//    backButton.frame = CGRectMake(backButton.frame.origin.x, backButton.frame.origin.y, (textSize.width + (backButtonCapWidth * 1.5)) > MAX_BACK_BUTTON_WIDTH ? MAX_BACK_BUTTON_WIDTH : (textSize.width + (backButtonCapWidth * 1.5)), backButton.frame.size.height);
//    
//    // Set the text on the button
//    [backButton setTitle:text forState:UIControlStateNormal];
//}
//
//- (void)dealloc
//{
//
//    [goldLab release];
//    [lvLab release];
//    [allExLab release];
//    [selectGoldImg release];
//    [navigationBarBackgroundImage release];
//    [navigationController release];
//    [super dealloc];
//}

@end